home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) Silicon Graphics, Inc. 1996 */
-
- /* multiwin.c - Demonstrates the use of multiple windows.
- * Use the mouse's position in the window to
- * determine the location of the eye position.
- *
- * Escape Key - exit the program
- * `f' Key - toggle filled / wireframe cylinder
- *
- * Left Mousebutton, down - start moving the eye
- * Left Mousebutton, up - stop moving the eye
- * Mouse pointer location - position eye
- * Middle Mousebutton, down - zoom in
- * Right Mousebutton, down - zoom out
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h> /* for fmodf */
- #include <stdio.h> /* for printf */
- #include <stdlib.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- enum { FRONT_VIEW, RIGHT_VIEW, TOP_VIEW, POLAR_VIEW };
-
- static char *windowTitles[] = {
- "front view", "right view", "top view", "polar view" };
-
- static GLint windowIDs[POLAR_VIEW + 1];
-
- static GLint eventWindow;
-
- static GLUquadricObj *quadObj;
-
- static GLboolean filledFlag = GL_FALSE;
-
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
- static GLint action;
-
- static GLdouble xStart = 0.0, yStart = 0.0;
-
- static GLfloat distance, twistAngle, incAngle, azimAngle;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height, windowHeight, windowWidth;
- GLint windowID, i;
- GLint xOrigin, yOrigin;
- GLint borderSize = 8;
- GLint titleHeight = 24;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- windowWidth = width / 2 - 2 * borderSize;
- windowHeight = height / 2 - 2 * borderSize - titleHeight;
-
- for ( i = 0; i < 4; i++ ) {
- /* works for up to four windows, places windows like this:
- * 0 1
- * 2 3
- */
- xOrigin = (i % 2)*width / 2 + borderSize;
- yOrigin = (i > 1)*(height / 2 + borderSize) + titleHeight;
- glutInitWindowPosition( xOrigin, yOrigin );
- glutInitWindowSize( windowWidth, windowHeight );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- windowIDs[i] = glutCreateWindow(windowTitles[i]);
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
- }
-
- quadObj = gluNewQuadric ();
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrate how to create multiple windows\n"
- "Left Mousebutton - move eye position in polarview window\n"
- "Middle Mousebutton - change twist angle in polarview window\n"
- "Right Mousebutton - move up/down to zoom in/out in all windows\n"
- "<f> Key - toggle wireframe/solid cylinder\n"
- "<R> Key - reset viewpoint\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glShadeModel( GL_FLAT );
- glEnable( GL_DEPTH_TEST );
-
- resetView();
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 7.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- redisplayAllWindows( GLvoid )
- {
- int i;
-
- /* update all windows views */
- for ( i = 0; i < 4; i++ ) {
- glutSetWindow( windowIDs[i] );
- glutPostRedisplay();
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'f': /* toggle filled mode */
- filledFlag = !filledFlag;
- redisplayAllWindows();
- break;
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- glutPostRedisplay();
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- glutPostRedisplay();
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- redisplayAllWindows();
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
- }
-
- void
- resetView( GLvoid )
- {
- distance = 4.5;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- drawGrid( GLvoid )
- {
- GLfloat x, y;
-
- glBegin( GL_LINES );
- for ( x = -1.0; x < 1.1; x += 0.1 ) {
- glVertex2f( x, -1.0 );
- glVertex2f( x, 1.0 );
- }
- for ( y = -1.0; y < 1.1; y += 0.1 ) {
- glVertex2f( -1.0, y );
- glVertex2f( 1.0, y );
- }
- glEnd();
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- GLint windowID = glutGetWindow();
-
- static GLfloat whiteColor[] = { 1.0, 1.0, 1.0 };
- static GLfloat redColor[] = { 1.0, 0.0, 0.0 };
- static GLfloat darkGreenColor[] = { 0.0, 0.5, 0.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- if ( windowID == windowIDs[FRONT_VIEW] )
- polarView( distance, 0, 90, 0 );
- else if ( windowID == windowIDs[RIGHT_VIEW] )
- polarView( distance, 0, 90, -90 );
- else if ( windowID == windowIDs[TOP_VIEW] )
- polarView( distance, 0, 0, 0 );
- else if ( windowID == windowIDs[POLAR_VIEW] )
- polarView( distance, azimAngle, incAngle, twistAngle );
-
- glColor3fv( redColor );
- glutSolidCone( 0.3, 0.6, 15, 15 );
-
- glColor3fv( darkGreenColor );
- glPushMatrix();
- glTranslatef( 0.8, -0.65, 0.0 );
- glRotatef( 30.0, 1.0, 0.5, 1.0 );
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- glTranslatef( 0.0, -0.0, -0.3 );
- if ( filledFlag == GL_TRUE )
- gluQuadricDrawStyle(quadObj, GLU_FILL);
- else
- gluQuadricDrawStyle(quadObj, GLU_LINE);
- gluCylinder(quadObj, 0.3, 0.3, 0.6, 12, 2);
- glPopMatrix();
-
- glColor3fv( whiteColor );
- drawGrid();
-
- glPopMatrix();
- glutSwapBuffers();
-
- }
-